package me.luger.core.commom.util;

import org.apache.log4j.MDC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * LogUtil
 *
 * @author luger
 * @date 2016/8/1 0001
 */
public class LogUtil {
    private static final String REQID = "reqid";

    private static final String LASTREQID = "lastreqid";

    private static final String MDC_TYPE = "type";

    private static final String MDC_PROJECT_ID = "project_id";

    private static final String MDC_SERVICE_ID = "service_id";

    private static final String MDC_MODULE_ID = "module_id";

    private static final String EMPTY_STRING = "";

    /**
     * 用户访问服务日志
     */
    private static final Logger ACCESS_LOGGER = LoggerFactory
            .getLogger("access-logger");


    /**
     * 异常日志
     */
    private static final Logger ERROR_LOGGER = LoggerFactory
            .getLogger("error-logger");

    /**
     * 调试日志
     */
    private static final Logger DEBUG_LOGGER = LoggerFactory
            .getLogger("debug-logger");

    private static final Logger INFO_LOGGER = LoggerFactory
            .getLogger("info-logger");

    /**
     * 管理平台类操作日志
     */
    private static final Logger OP_LOGGER = LoggerFactory
            .getLogger("op-logger");

    public static void debugLog(String msg,Object... objects) {
        DEBUG_LOGGER.debug(msg,objects);
    }


    public static void errorLog(String msg) {
        ERROR_LOGGER.error(msg);
    }

    public static void errorLog(Throwable e, String msg) {
        ERROR_LOGGER.error(e.toString()+msg);
        e.printStackTrace();
    }

    public static void infoLog(String msg){
        INFO_LOGGER.info(msg);
    }


    public static void opLog(String msg){
        OP_LOGGER.info(msg);
    }


    public static void accessLog(String msg) {
        ACCESS_LOGGER.info(msg);
    }

    private static String getMDCValue(String key) {
        Object value = MDC.get(key);
        if (value != null) {
            return (String) value;
        } else {
            return EMPTY_STRING;
        }
    }

    /**
     * 获取日至编号 未在工具类中提供添加日志编号的方法，日志ID不能被业务干预。
     *
     * @return
     */
    public static String getLogId() {
        return getMDCValue(REQID);
    }

    public static void setLogId(String value) {
        Object o = new Object();
        MDC.put("22",o);
        MDC.put("22","444");
        MDC.put(REQID, value);
    }

    public static String getLastLogId() {
        return getMDCValue(LASTREQID);
    }

    public static void setLastLogId(String value) {
        MDC.put(LASTREQID, value);
    }

    public static String getLogType() {
        return getMDCValue(MDC_TYPE);
    }

    public static void setLogType(String value) {
        MDC.put(MDC_TYPE, value);
    }

    public static String getProjectId() {
        return getMDCValue(MDC_PROJECT_ID);
    }

    public static void setProjectId(String value) {
        MDC.put(MDC_PROJECT_ID, value);
    }

    public static String getServiceId() {
        return getMDCValue(MDC_SERVICE_ID);
    }

    public static void setServiceId(String value) {
        MDC.put(MDC_SERVICE_ID, value);
    }

    public static String getModuleId() {
        return getMDCValue(MDC_MODULE_ID);
    }

    public static void setModuleId(String value) {
        MDC.put(MDC_MODULE_ID, value);
    }

    public static void cleanMDC() {
        MDC.clear();
    }
}
